const Koa = require('koa');
const http = require('http');
const socketIO = require('socket.io');
const app = new Koa();
const server = http.createServer(app.callback());
const io = socketIO(server, {
    cors: {
        origin: '*',
        methods: ['GET', 'POST'],
        allowedHeaders: ['Origin', 'Content-Type', 'Accept', 'token'],
        credentials: true,
    }
});
io.use((socket, next) => {
    // const user = socket.request.state.user; // 从请求中获取用户信息`
    return next()
    let token = socket.request.headers.token
    if (token) {
        return next(); // 验证通过，允许连接
    } else {
        return next(new Error('Unauthorized')); // 验证失败，拒绝连接
    }
});
io.on('connection', (socket) => {
    console.log('启动成功');
    socket.on('create_room', (data) => {
        socket.join(data)
        socket.to("123").emit("join-room", '加入')
    })
    socket.on("send-message", (message) => {
        socket.to("123").emit("chat-message", {
            message: message,
            name: 11,
        });
    });

});

server.listen(3030, () => {
});